package com.intelligentmeterreadingsys.otherequipment.activity;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.otherequipment.db.EquipementDao;
import com.intelligentmeterreadingsys.povo.OtherEquipmentPOVO;
import com.intelligentmeterreadingsys.waterlist.copywater.activity.ImageSwitcherActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * Rfid读卡异常 设备巡检
 * @author king
 *
 */
public class EquipementCheckNoRfidActivity extends Activity implements OnClickListener{
	public ProgressDialog pd;// 读卡进度条
	public boolean isStop = true;
	public boolean issave = false;
	public long time;
	public boolean isupload = false;
	public OtherEquipmentPOVO eq=null;
	public TextView textview1;// 设备编号
	public TextView textview2;// 设备类型
	public TextView textview3;// 安装时间
	public TextView textview4;// 安装地址
	public TextView textview5;// 上次巡检日期
	public TextView textview6;// 上次巡检结果
	public Spinner spinner;// 设备状态
	public EditText edit;// 巡检结果
	public Button button1;// 拍照
	public Button button2;// 保存
	public Button button3;// 上传
    public String RfidNumber="";
    public String checktype="正常";
    public String[] date=new String[]{"正常","异常"};
    public String equipementId="";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other_equipment_check);
		init();
		Intent intents=getIntent();
		equipementId=intents.getStringExtra("equipementId");
	    EquipementDao dao=new EquipementDao(this);
	    eq=dao.queryCheckEquipment(equipementId, null);
	    dao.close();
		
	}

	/**
	 * 初始化控件
	 */
	public void init() {
		textview1 = (TextView) findViewById(R.id.other_equipment_check_number);
		textview2 = (TextView) findViewById(R.id.other_equipment_check_type);
		textview3 = (TextView) findViewById(R.id.other_equipment_check_date);
		textview4 = (TextView) findViewById(R.id.other_equipment_check_adds);
		textview5 = (TextView) findViewById(R.id.other_equipment_check_lastcheckdate);
		textview6 = (TextView) findViewById(R.id.other_equipment_check_lastcheckresult);
		button1 = (Button) findViewById(R.id.other_equipment_check_paizhao);
		button2 = (Button) findViewById(R.id.other_equipment_check_save);
		button3 = (Button) findViewById(R.id.other_equipment_check_upload);
		spinner = (Spinner) findViewById(R.id.other_equipment_check_equipmentstatus);
		edit = (EditText) findViewById(R.id.other_equipment_check_thisequipmentresult);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
	   ArrayAdapter<String> ada1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,date);//放数据
	   ada1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//设置打开方式
       spinner.setAdapter(ada1);
       
       spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			checktype=arg0.getItemAtPosition(arg2).toString();
			
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		      
		}
	});
             
	}
	/**
	 * 展现数据
	 */
	public void viewdate(){
		textview1.setText("");
		textview2.setText("");
		textview3.setText("");
		textview4.setText("");
		textview5.setText("");
		textview6.setText("");
		edit.setText("");
		if(eq!=null){
			textview1.setText(eq.getNumber());
			textview2.setText(eq.getTypename());
			textview3.setText(eq.getDate());
			textview4.setText(eq.getAdds());
			textview5.setText(eq.getLastcheckdate());
			textview6.setText(eq.getLastcheckresult());
			edit.setText(eq.getCheckdesc());
			if(eq.getChecktype()!=null){
				if(eq.getChecktype().equals("正常")){
					spinner.setSelection(0, true);
				}else{
					spinner.setSelection(1, true);
				}
				
			}
			
			
		}
	}

	@Override
	public void onClick(View v) {
		Intent intent=null;
		if(eq==null){
			Toast.makeText(EquipementCheckNoRfidActivity.this, "没有找到设备不能进行此操作", Toast.LENGTH_SHORT).show();
			return;
		}
		switch (v.getId()) {
		case R.id.other_equipment_check_paizhao:
			 intent=new Intent(this, ImageSwitcherActivity.class);
			 Bundle bundle=new Bundle();
			 bundle.putString("otherid", eq.getId());
			 bundle.putString("status", "5");
			 bundle.putString("classname", "EquipementRfidCheckActivity");
			 intent.putExtras(bundle);
			 startActivity(intent);
			break;
		case R.id.other_equipment_check_save:
			  EquipementDao dao=new EquipementDao(this);
			  if(!dao.isPhotograph(eq.getId())){
				  Toast.makeText(EquipementCheckNoRfidActivity.this, "这种情况须拍照！", Toast.LENGTH_SHORT).show();
				  return;
			  };
				issave= dao.checkEquipement(checktype, edit.getEditableText().toString(), eq.getId(),"1");
				if(issave){
					Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
				}
			break;
		case R.id.other_equipment_check_upload:
			
			break;
		default:
			break;
		}
		
	}
}
